<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket 测试</title>
</head>
<body>
    <input type="text" id="message">
    <button id = "testbutton">发送</button>

    <script>
    let websocket = new WebSocket("ws://127.0.0.1:8080/test");
    
    websocket.onopen = function(){
        console.log("连接建立");
    }
    websocket.onmessage = function(me){
        console.log("消息传递 message = " + me.data);
        
    }
    websocket.onerror = function(){
        console.log("连接异常中断");
        
    }
    websocket.onclose = function(){
        console.log("连接中断");
        
    }
     let messageInput = document.querySelector('#message');
        let sendButton = document.querySelector('#testbutton');
        sendButton.onclick = function() {
            console.log("websocket 发送消息: " + messageInput.value);
            websocket.send(messageInput.value);
        }
</script>
</body>

</html>